<?php
namespace app\user\controller\admin;

use app\admin\controller\Backend;

class Index extends Backend
{

    protected $model;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('user/user', 'service');
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name'))
            {
                return $this->selectpage();
            }
            
            list($where, $sort, $order, $offset, $limit, $field, $extra) = $this->buildparams();
            $where[] = 'role=0';
            $where = function($query) use ($where) {
                foreach ($where as $k => $v)
                {
                    if (is_array($v)) {
                        call_user_func_array([$query, 'where'], $v);
                    } else {
                        $query->where($v);
                    }
                }
            };
            $result = $this->model->lists($where,$limit,$offset, (!$sort && !$order ? '' : $sort.' '.$order), $field, $extra);
            return $this->success('Operation completed', null, $result);
        }
        return $this->view->fetch();
    }

    /**
     * 分销商列表
     */
    public function dist()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name'))
            {
                return $this->selectpage();
            }
            
            list($where, $sort, $order, $offset, $limit, $field, $extra) = $this->buildparams();
            $where[] = 'role=1';
            $where = function($query) use ($where) {
                foreach ($where as $k => $v)
                {
                    if (is_array($v)) {
                        call_user_func_array([$query, 'where'], $v);
                    } else {
                        $query->where($v);
                    }
                }
            };
            $result = $this->model->lists($where,$limit,$offset, (!$sort && !$order ? '' : $sort.' '.$order ), $field, $extra);
            return $this->success('Operation completed', null, $result);
        }
        return $this->view->fetch();
    }

    /**
     * 客户经理列表
     */
    public function customer()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name'))
            {
                return $this->selectpage();
            }
            
            list($where, $sort, $order, $offset, $limit, $field, $extra) = $this->buildparams();
            $where[] = 'role=2';
            $where = function($query) use ($where) {
                foreach ($where as $k => $v)
                {
                    if (is_array($v)) {
                        call_user_func_array([$query, 'where'], $v);
                    } else {
                        $query->where($v);
                    }
                }
            };

            $attrs = $this->request->param('attrs') ? explode(",", $this->request->param('attrs')) : [];
            $result = $this->model->lists($where,$limit,$offset, (!$sort && !$order ? '' : $sort.' '.$order ), $field, $extra, [], $attrs);
            return $this->success('Operation completed', null, $result);
        }
        return $this->view->fetch();
    }

}
